package org.dochandler.faces.bean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.dochandler.config.DHConfig;
import org.dochandler.faces.servicefactory.OfficerServiceFacesFactory;
import org.dochandler.faces.util.FacesBean;
import org.dochandler.faces.util.FacesNav;
import org.dochandler.service.OfficerService;
import org.dochandler.service.domain.Document;
import org.dochandler.service.exception.DocumentNotFoundException;
import org.esfinge.guardian.exception.AuthorizationException;

@SuppressWarnings("serial")
@ManagedBean
@RequestScoped
public class DocumentBean implements Serializable {
	
	private Long documentId;
	private String title;
	private String content;
	private Date creationDate;
	
	private OfficerService officerService;
	
	/**
	 * Constructor
	 */
	public DocumentBean() {
		setOfficerService(OfficerServiceFacesFactory.getInstance().create(getFacesCtx()));
	}
	
	/**
	 * Requests to the service layer that an specific document be accessed 
	 * @return
	 */
	public String accessDocument() {
		String pageKey = FacesNav.SHOW_DOC.pageKey();
		Document document = null;
		try {
			document = getOfficerService().accessDocument(documentId);
			getFacesCtx().getExternalContext().getSessionMap().put(FacesBean.DOC.beanKey(), document);
		} catch(DocumentNotFoundException e) {
			String message = "Document number " + documentId + " could not be found on our database.";
			getFacesCtx().addMessage("main_form", new FacesMessage(message));
			pageKey = FacesNav.DOC_NOT_FOUND.pageKey();
		} catch(AuthorizationException e) {
			String authorizationMsg = DHConfig.getString("msg_unauthorized");
			getFacesCtx().addMessage("main_form", new FacesMessage(authorizationMsg));
			pageKey = FacesNav.UNAUTHORIZED.pageKey();
		} catch(Exception e) {
			String message = "Some error have occured. Please contact the system administrator for checking this out.";
			getFacesCtx().addMessage("main_form", new FacesMessage(message));
			pageKey = FacesNav.FUNCTIONS_LIST.pageKey();
		}
		return pageKey;
	}
	
	/**
	 * Requests to the service layer that all documents be listed to the requester
	 * @return
	 */
	public String listDocuments() {
		String pageKey = FacesNav.LIST_DOCS.pageKey();
		
		try {
			List<Document> listDocs = getOfficerService().listDocuments();
			getFacesCtx().getExternalContext().getSessionMap().put(FacesBean.DOCS.beanKey(), listDocs);
			
		} catch (AuthorizationException e) {
			String authorizationMsg = DHConfig.getString("msg_unauthorized");
			getFacesCtx().addMessage("main_form", new FacesMessage(authorizationMsg));
			pageKey = FacesNav.FUNCTIONS_LIST.pageKey();
		}
		return pageKey;
	}

	/**
	 * Requests the creation of a new document
	 * @return
	 */
	public String createDocument() {
		String actionForward = "";
		getOfficerService().createDocument(null);
		return actionForward;
	}
	
	
	private FacesContext getFacesCtx() {
		return FacesContext.getCurrentInstance();
	}
	
	/*
	 * SETTERS AND GETTERS
	 */

	public Long getId() {
		return documentId;
	}

	public void setId(Long id) {
		this.documentId = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public OfficerService getOfficerService() {
		return officerService;
	}

	public void setOfficerService(OfficerService officerService) {
		this.officerService = officerService;
	}
	
}
